//
//  ViewController.m
//  UI24_KVO_ NSNotificationCenter
//
//  Created by Scott on 16/1/14.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "ViewController.h"


#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface ViewController () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UICollectionView *collectView;

@property (nonatomic, retain) UIView *viewForRedline;

@property (nonatomic, retain) UICollectionView *collectExten;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self createRedLine]; /** 滑动红色线条. */
    [self createCollectView]; /** collectionView. */
    [self createKVOAction]; /**< KVO. */
}


#pragma mark - SubViews
- (void)createRedLine {
    
    self.viewForRedline = [[UIView alloc] initWithFrame:CGRectMake(0, 50, WIDTH / 5, 2)];
    self.viewForRedline.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.viewForRedline];
}


#pragma mark - CollectionView Park
- (void)createCollectView {
    
    // layout
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    flowLayout.itemSize = CGSizeMake(WIDTH, HEIGHT - 52);
    
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    
    
    // collection
    self.collectView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50 + 2, WIDTH, HEIGHT - 50 - 2) collectionViewLayout:flowLayout];
    [self.view addSubview:self.collectView];
    
    
    self.collectView.dataSource = self;
    self.collectView.delegate = self;
    
    self.collectView.backgroundColor = [UIColor whiteColor];
    
    self.collectView.pagingEnabled = YES;
    
    // regis
    [self.collectView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"poolForCell"];
    
    
}



#pragma mark  Collection DataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

        return 5;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (collectionView == self.collectView) {
        
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolForCell" forIndexPath:indexPath];
        
        cell.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.f green:arc4random() % 256 / 255.f blue:arc4random() % 256 / 255.f alpha:1.f];
        
        return cell;
        
    } else {
        
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolForCellExten" forIndexPath:indexPath];
        return cell;
    }
}



#pragma mark - 知识点1 KVO(键值观察者)
/** 核心: 观察者 观察 某一个对象的属性变化. */
- (void)createKVOAction {
    
    // VC观察collection的contentOffset属性.
    [self.collectView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:@"ddd"];
}


/** 当被观察值属性发生变化的时候, 调用此方法. */
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    
    CGFloat x = [[change objectForKey:@"new"] CGPointValue].x;
    
    CGFloat redX = x * WIDTH / 5 / WIDTH;
    
    // 修改Redline位置.
    self.viewForRedline.transform = CGAffineTransformMakeTranslation(redX, 0);
    
}


#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
